//
//  Int+Ex.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/20.
//

public extension Int {
    public var localNumber: String? {
        return localNumber()
    }
    
    public func localNumber(_ minimumIntegerDigits: Int? = nil, locale: Locale? = nil) -> String? {
        let formatter = NumberFormatter()
        if let locale = locale {
            formatter.locale = locale
        }
        else {
            formatter.locale = Locale.local
        }
        
        formatter.numberStyle = .decimal
        if let value = minimumIntegerDigits {
            formatter.minimumIntegerDigits = value
        }
        return formatter.string(for: self);
    }
}
public extension Locale {
    public static var local: Locale {
        return Locale(identifier: TSConfig.appLanguage)
    }
}

public extension Int {
    /// 将秒数转换为 "HH:mm" 格式的字符串
    /// - Returns: 转换后的字符串
    public func toTimeString() -> String {
        // 计算小时和分钟
        let hours = self / 3600
        let minutes = (self % 3600) / 60
        
        // 格式化为 "HH:mm"
        return String(format: "%02d:%02d", hours, minutes)
    }
    
    /// 将秒数转换为 "HH:mm:ss" 格式的字符串
    /// - Returns: 转换后的字符串
    public func toHourMinuteSecondString() -> String {
        // 计算小时、分钟和秒
        let hours = self / 3600
        let minutes = (self % 3600) / 60
        let seconds = self % 60
        
        // 格式化为 "HH:mm:ss"
        return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
    }
    
    public static func timestampInt() -> Int {
         let currentTimestamp = Int(Date().timeIntervalSince1970)
         return currentTimestamp
     }
}

public extension Int {
    private static var uniqueCounter: Int64 = 0
    
    static var uuid: Int {
        return Int(OSAtomicIncrement64(&uniqueCounter))
    }
}

public extension TimeInterval {
    public var mmss: String {
        let minute = Int(self) / 60
        let second = Int(self) % 60
        return String(format: "%02d", minute) + ":" + String(format: "%02d", second)
    }

    public var mmsshh: String {
        let hour = Int(self) / 3600
        let minute = (Int(self) % 3600) / 60
        let second = Int(self) % 60

        if hour > 0 {
            return String(format: "%02d:%02d:%02d", hour, minute, second)
        } else {
            return mmss
        }
    }

    public var hhmmss: String {
        let hour = Int(self) / 3600
        let minute = (Int(self) % 3600) / 60
        let second = Int(self) % 60
        return String(format: "%02d:%02d:%02d", hour, minute, second)
    }
}
public extension Int {
    public var mmss: String {
        let minute = Int(self) / 60
        let second = Int(self) % 60
        return String(format: "%02d", minute) + ":" + String(format: "%02d", second)
    }

    public var mmsshh: String {
        let hour = Int(self) / 3600
        let minute = (Int(self) % 3600) / 60
        let second = Int(self) % 60

        if hour > 0 {
            return String(format: "%02d:%02d:%02d", hour, minute, second)
        } else {
            return mmss
        }
    }

    public var hhmmss: String {
        let hour = Int(self) / 3600
        let minute = (Int(self) % 3600) / 60
        let second = Int(self) % 60
        return String(format: "%02d:%02d:%02d", hour, minute, second)
    }
}


public extension Int {
    /// 将字节数转换为合适的单位字符串
//    var sizeString: String {
//        if self < 1024 {
//            return "\(self) B"
//        } else if self < 1024 * 1024 {
//            return "\(String(format: "%.1f", Double(self) / 1024.0)) KB"
//        } else {
//            return "\(String(format: "%.1f", Double(self) / (1024.0 * 1024.0))) MB"
//        }
//    }
    //
    var sizeAppleString: String {
        if self < 1000 {
            return "\(self) B"
        } else if self < 1000 * 1000 {
            return "\(String(format: "%.1f", Double(self) / 1000.0)) KB"
        } else {
            return "\(String(format: "%.1f", Double(self) / (1000.0 * 1000.0))) MB"
        }
    }
}
